package com.wlz.security.service;

import com.wlz.security.config.WebSecurityConfig1;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 *  方式2
 *      用户密码 不加密 noop
 */
@ConditionalOnBean(WebSecurityConfig1.class)
@Service
public class MyUserDetailsService1 implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        // Spring security5中新增加了加密方式，并把原有的spring security的密码存储格式改了，修改后的密码
        //存储格式为：{id}encodedPassword 如果密码不指定{id}会抛异常：
        // java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
        // {noop}123456 不使用加密方式
        UserDetails userDetails = new User("wlz", "{noop}123456",
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,user"));

        return userDetails;
    }
}
